﻿Imports System.ServiceModel.DomainServices.Client

Partial Public Class FullDeCaixaAltaModificacio
    Inherits ChildWindow

    Private _LLibreCaixa As Web.LLibreCaixa
    Public ReadOnly Property LLibreCaixa As Web.LLibreCaixa
        Get
            Return _LLibreCaixa
        End Get
    End Property

    Private _Mode As EnumMode
    Public ReadOnly Property Mode As EnumMode
        Get
            Return _Mode
        End Get
    End Property

    Public Enum EnumMode
        ModeAlta
        ModeModificacio
    End Enum

#Region "Crear i Destruir"
    Public Sub New(pMode As EnumMode, pLLibreCaixa As Web.LLibreCaixa)
        MyBase.New()

        InitializeComponent()
        _Mode = pMode
        _LLibreCaixa = pLLibreCaixa
    End Sub


    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub FullDeCaixaAlta_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then
            Me.Activity.IsActive = True

            AddHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats
            Contexte.LoadUsuaris()

            AddHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats
            Contexte.LoadHostals()

            ToolTipService.SetToolTip(Me.cboHostals, Application.Current.Host.Source.Host)

            If Mode = EnumMode.ModeAlta Then
                _LLibreCaixa.CaixaInicial = 0
                _LLibreCaixa.Data = Date.SpecifyKind(Now.Date, DateTimeKind.Unspecified)
                _LLibreCaixa.DataHoraObertura = Date.SpecifyKind(Now, DateTimeKind.Unspecified)
                _LLibreCaixa.Usuari = Contexte.UsuariActual
            Else
                Me.Title = StringLibrary.EditarFullDeCaixa

                Me.cboHostals.IsEnabled = False
                Me.cboUsuaris.IsEnabled = False
                Me.txtData.IsEnabled = False
            End If

            Me.gridDetall.DataContext = _LLibreCaixa

        End If
    End Sub
#End Region

#Region "Carregar Dades"
    Private _HostalsCarregats As Boolean = False
    Private _UsuarisCarregats As Boolean = False

    Private Sub UsuarisCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats

        If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
            cboUsuaris.ItemsSource = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari = EnumTipusUsuari.Administrador OrElse U.IdTipusUsuari = EnumTipusUsuari.Recepcionista).ToList
        Else
            cboUsuaris.ItemsSource = {Contexte.UsuariActual}
        End If

        _UsuarisCarregats = True
        If _HostalsCarregats And _UsuarisCarregats Then Me.Activity.IsActive = False
    End Sub

    Public Sub HostalsCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats

        If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
            Me.cboHostals.ItemsSource = Contexte.Hostals
        Else
            Me.cboHostals.ItemsSource = Contexte.HostalsPerUsuariActual.AsEnumerable
        End If

        If Me.cboHostals.Items.Count > 0 AndAlso Mode = EnumMode.ModeAlta Then _LLibreCaixa.Hostal = DirectCast(Me.cboHostals.Items(0), Web.Hostal)

        _HostalsCarregats = True
        If _HostalsCarregats And _UsuarisCarregats Then Me.Activity.IsActive = False
    End Sub


#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() And Validacio.Errors.Count = 0 Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            For Each E As Entity In s.EntitiesInError
                For Each VError As ComponentModel.DataAnnotations.ValidationResult In E.ValidationErrors
                    If s.EntitiesInError.Count = 1 AndAlso E.ValidationErrors.Count = 1 AndAlso VError.MemberNames.Count = 1 AndAlso VError.MemberNames(0) = "ControlarLLibreObertAnterior" Then
                        If MessageBox.Show(VError.ErrorMessage & vbNewLine & StringLibrary.SeguroNuevo, StringLibrary.Confirmar, MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
                            's.MarkErrorAsHandled()
                            LLibreCaixa.ControlarLLibreObertAnterior = False
                            OKButton_Click(Nothing, Nothing)
                        Else
                        End If
                    Else
                        Me.Validacio.Errors.Add(New ValidationSummaryItem(VError.ErrorMessage))
                    End If
                Next
            Next
            s.MarkErrorAsHandled()

        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub
#End Region


End Class
